/*
 * @(#) $Header$
 *
 * Copyright (C) 2012  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package ca.forklabs.baselib.util.comparator;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import ca.forklabs.baselib.util.Arrays;

/**
 * Class {@code ChainComparator} is a composite comparator. Objects are compared
 * using, in order, the comparators until they are no longer equals.
 *
 * @param   <T>   the type of object compared.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.util.comparator.ChainComparator">Daniel Léonard</a>
 * @version $Revision$
 */
public class ChainComparator<T> implements Comparator<T> {

//---------------------------
// Instance variable
//---------------------------

   /** The list of comparators. */
   private List<Comparator<T>> comparators = new LinkedList<>();


//---------------------------
// Constructor
//---------------------------

    /**
     * Constructor.
     * @param   comparators   the initial comparators.
     */
   @SafeVarargs
    public ChainComparator(Comparator<T>... comparators) {
       this(Arrays.iterable(comparators));
       }

    /**
     * Constructor.
     * @param   comparators   the initial comparators.
     */
    public ChainComparator(Iterable<Comparator<T>> comparators) {
       this.addComparators(comparators);
       }


//---------------------------
// Accessors and mutators
//---------------------------

   /**
    * Gets the comparators.
    * @return   the comparators.
    */
   protected List<Comparator<T>> getComparators() {
      return this.comparators;
      }


//---------------------------
// Instance method
//---------------------------

   /**
    * Adds a comparator at the end of the list.
    * @param   comparator   a new comparator.
    */
   @SuppressWarnings("hiding")
   public void addFilter(Comparator<T> comparator) {
      List<Comparator<T>> comparators = this.getComparators();
      comparators.add(comparator);
      }

   /**
    * Adds multiple comparators at the end of the list.
    * @param   comparators   the new comparators.
    */
   @SuppressWarnings("hiding")
   public void addComparators(Iterable<Comparator<T>> comparators) {
      for (Comparator<T> comparator : comparators) {
         this.addFilter(comparator);
         }
      }


//---------------------------
// Implemented methods from java.util.Comparator
//---------------------------

   /**
    * Compares the two objects. The pair of object is compared with each
    * comparator, in order, until they are no longer equals.
    * @param   t1   the first object.
    * @param   t2   the second object.
    * @return   a negative value if {@code t1 &lt; t2}, zero if {@code t1 == t2}
    *           and a positive value if {@code t1 &gt; t2}.
    * @see   Comparator#compare(Object, Object)
    */
   @Override
   @SuppressWarnings("hiding")
   public int compare(T t1, T t2) {
      int comparison = 0;

      List<Comparator<T>> comparators = this.getComparators();
      for (Comparator<T> comparator : comparators) {
         comparison = comparator.compare(t1, t2);
         if (0 != comparison) {
            break;
            }
         }

      return comparison;
      }


//---------------------------
// Class methods
//---------------------------

   /**
    * Creates a new chain comparator.
    * @param   comparators   the comparators from the most significative.
    * @param   <T>   the type being compared.
    * @return   a chain comparator.
    */
   @SafeVarargs
   public static <T> ChainComparator<T> create(Comparator<T>... comparators) {
      ChainComparator<T> comparator = new ChainComparator<>(comparators);
      return comparator;
      }

   /**
    * Creates a new chain comparator.
    * @param   comparators   the comparators from the most significative.
    * @param   <T>   the type being compared.
    * @return   a chain comparator.
    */
   public static <T> ChainComparator<T> create(Iterable<Comparator<T>> comparators) {
      ChainComparator<T> comparator = new ChainComparator<>(comparators);
      return comparator;
      }

   }
